package corejava;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;

/**
 * @Author: YLBG-YCY-1325
 * @Description: 反射
 * @Date: 2017/3/31
 */
public class ReflectionTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入类的全称(e.g. java.util.Date):");
        String className = scanner.next();

        try {
            Class cls = Class.forName(className);
            Class superClass = cls.getSuperclass();
            String modifier = Modifier.toString(cls.getModifiers());
            // 获取类前面的修饰符
            if (modifier.length() > 0) {
                System.out.printf(modifier + " ");
            }
            System.out.printf("class" + className);
            // 超类继承
            if (null != superClass && superClass != Object.class) {
                System.out.printf(" extend " + superClass.getName());
            }
            System.out.printf("\n{\n");
            printFileds(cls);
            System.out.println();
            printContructors(cls);
            System.out.println();
            printMethods(cls);
            System.out.println("}");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.exit(0);
    }

    /**
     * 打印类的所有构造器
     * @param cls 反射类
     */
    public static void printContructors(Class cls) {
        Constructor[] constructors = cls.getConstructors();
        for (Constructor constructor : constructors) {
            String modifier = Modifier.toString(constructor.getModifiers());
            if (modifier.length()>0){
                System.out.printf(modifier+" ");
            }
            System.out.printf(constructor.getName()+"(");

            Class[] paramsType = constructor.getParameterTypes();
            for (int j=0;j<paramsType.length;j++){
                if (j>0){
                    System.out.printf(",");
                }
                System.out.printf(paramsType[j].getName());
            }
            System.out.printf(");\n");
        }
    }

    /**
     * 打印所有方法
     * @param cls 反射类
     */
    public static void printMethods(Class cls){
        Method[] methods = cls.getMethods();

        for (Method method:methods){
            String modifier = Modifier.toString(method.getModifiers());
            if (modifier.length()>0){
                System.out.printf(modifier+" ");
            }
            //获取返回类型
            Class returnType = method.getReturnType();
            System.out.printf(returnType.getName()+" ");
            System.out.printf(method.getName()+"(");
            //获取参数类型
            Class[] paramType = method.getParameterTypes();
            for (int i=0;i<paramType.length;i++){
                if (i>0){
                    System.out.printf(",");
                }
                System.out.printf(paramType[i].getName());
            }
            System.out.printf(");\n");
        }
    }


    /**
     * 打印类的所有属性
     * @param cls 反射类
     */
    private static void  printFileds(Class cls){
        Field[] fields = cls.getDeclaredFields();
        for (Field field:fields){
            String modifier = Modifier.toString(field.getModifiers());
            Class fieldType = field.getType();
            if (modifier.length()>0){
                System.out.printf(modifier + " ");
            }
            System.out.printf(fieldType+" ");
            System.out.printf(field.getName()+";\n");
        }
    }

}
